//
//  GuoguoReactionView.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/18.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "GuoguoReactionView.h"
#import "Masonry.h"
#import "GuoguoChannelLabel.h"

@interface GuoguoReactionView ()

@property (nonatomic, strong) UIScrollView *channelView;//频道View
@property (nonatomic, strong) UIView *underLine;//底部线条
@property (nonatomic, strong) GuoguoChannelLabel *selectedLabel;//选中的按钮
@property (nonatomic, strong) NSMutableArray *labelArray;//频道数组

@end
@implementation GuoguoReactionView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //设置UI
        [self setUpUI];
    }
    return self;
}
//设置UI
- (void)setUpUI {
    //添加控件
    [self addSubview:self.channelView];
    [self addSubview:self.underLine];
    //设置约束
    [self.channelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.top.equalTo(self).offset(0);
        make.height.mas_equalTo(44);
    }];
    
    [self.underLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2);
        make.leading.bottom.equalTo(self.channelView);
        make.width.mas_equalTo(100);
    }];
}

#pragma mark - 懒加载
//频道view
- (UIScrollView *)channelView {
    if (!_channelView) {
        _channelView = [[UIScrollView alloc] init];
        
        //创建按钮
        CGFloat buttonWidth = 100;
        CGFloat buttonHeight = 44;
        CGFloat buttonY = 0;
        
        self.labelArray = [NSMutableArray array];
        debugLog(@"%@", globalManager.cateListArr);
        for (int i = 0; i < globalManager.cateListArr.count; i++) {
            CGFloat buttonX = i * buttonWidth;
            GuoguoChannelLabel *button = [[GuoguoChannelLabel alloc] initWithFrame:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];
            button.text = globalManager.cateListArr[i];
            button.textAlignment = NSTextAlignmentCenter;
            button.tag = i;
            if (button.tag == 0) {
                button.scale = 1.0;
                self.selectedLabel = button;
            }
            [self.labelArray addObject:button];
            //添加事件
            [button addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];
            
            [_channelView addSubview:button];
        }
        _channelView.backgroundColor = [UIColor whiteColor];
        _channelView.bounces = NO;
        _channelView.contentSize = CGSizeMake(globalManager.cateListArr.count * buttonWidth, 0);
        _channelView.showsHorizontalScrollIndicator = NO;
    }
    return _channelView;
}

//底部线条
- (UIView *)underLine {
    if (!_underLine) {
        _underLine = [[UIView alloc] init];
        _underLine.backgroundColor = [UIColor redColor];
    }
    return _underLine;
}

#pragma mark - collectionViewDelegate
//监听滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self setUpWhenScroll:scrollView];
}

#pragma mark - 滚动时执行的方法
- (void)setUpWhenScroll:(UIScrollView *)scrollView {
    
    //1.计算一下,scrollView.contentOffset.x / scrollView.bounds.size.width;
    CGFloat value = (CGFloat)scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    if (value < 0 || value > (self.labelArray.count -1)) return;
    
    //2.左边的索引
    int leftIndex = (int)scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    //3.右边的索引
    int rightIndex = leftIndex + 1;
    
    //4.右边的缩放比率
    CGFloat rightScale = (value - leftIndex);
    
    //5.左边的缩放比率
    CGFloat leftScale = 1 - rightScale;
    
    //6.取出左边的ChannelLabel给它设置左边对应的缩放比率
    GuoguoChannelLabel *leftChannelLabel = self.labelArray[leftIndex];
    leftChannelLabel.scale = leftScale;
    
    //7.取出右边的ChannelLabel给它设置左边对应的缩放比率
    if (rightIndex < self.labelArray.count) {
        GuoguoChannelLabel *rightChannelLabel = self.labelArray[rightIndex];
        rightChannelLabel.scale = rightScale;
    }
}

#pragma mark - 点击事件
- (void)tapAction:(UITapGestureRecognizer *)recognizer {
    
    GuoguoChannelLabel *chanelLabel = (GuoguoChannelLabel *)recognizer.view;
    self.selectedLabel = chanelLabel;
    [self setChannellabelTextAndColor];
}

#pragma mark - 选中按钮文字处理方法
//设置channel文字变大、变红、居中的方发
- (void)setChannellabelTextAndColor {
    //1.计算channelView应该滚动多远
    CGFloat needScrollContentOffsetX = self.selectedLabel.center.x - self.channelView.bounds.size.width * 0.5;
    
    //1.1 重新设置,点击最左边的极限值
    if (needScrollContentOffsetX < 0) {
        needScrollContentOffsetX = 0;
    }
    CGFloat maxScrollContentOffsetX = self.channelView.contentSize.width - self.channelView.bounds.size.width;
    
    //1.2 重新设置,点击最右边的极限值
    if (needScrollContentOffsetX > maxScrollContentOffsetX) {
        needScrollContentOffsetX = maxScrollContentOffsetX;
    }
    
    //2.让其滚动
    [self.channelView setContentOffset:CGPointMake(needScrollContentOffsetX, 0) animated:YES];
    
    //3.选中的最大最红,没选中的最小最黑
    for (GuoguoChannelLabel *channelLabel in self.labelArray) {
        if (channelLabel == self.selectedLabel) {
            channelLabel.scale = 1.0; //变成最大最红
        }else{
            channelLabel.scale = 0.0; //变成最小最黑
        }
    }
    [self underLinePosition:needScrollContentOffsetX];
}

#pragma mark - 底线的动画
- (void)underLinePosition:(CGFloat)needScrollContentOffsetX {
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect tempFrame = self.underLine.frame;
        tempFrame.origin.x = self.selectedLabel.frame.origin.x - needScrollContentOffsetX;
        self.underLine.frame = tempFrame;
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
